// 登录
const onLogin = function () {
    let login = document.querySelector(".login")
    let usernameInput = document.querySelector('input[name="username"]')
    let passwordInput = document.querySelector('input[name="password"]')



    login.addEventListener('submit', function (e) {
        e = e || window.event
        e.preventDefault()

        let username = usernameInput.value
        let password = passwordInput.value

        // 非空验证
        if (username == "" || password == "") {
            alert('用户名不能为空！')
        }
        else  {
            myPromise({
                method: 'post',
                url: 'https://api.yuguoxy.com/api/member/login',
                data: {
                    username,
                    password         
                },
            }).then(res => {
                if (res.resultCode == 1) {
                    // alert('登录成功')
                    location.href = "../html/index.html"
                    let user = res.resultInfo
    
                     // 保存用户状态信息
                    let userInfo = {
                        username:user.username,
                        nick:user.nick,
                        password:user.password,
                        headerimg:user.headerimg
                    }
                    localStorage.setItem('user', JSON.stringify(userInfo))
           
                }
                 else if(res.resultCode == -1) {
                    alert('用户名或密码出错')
                }
            
            })
        }
        // 登录请求
        
    
    })
}
onLogin()